package prisd.brand.com.threegangswitch.util;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Build;
import android.os.Environment;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.text.TextUtils;

import java.io.File;
import java.lang.reflect.Method;
import java.util.Random;
import java.util.concurrent.Future;

public class PhoneUtil {
    public static final String IMEI = "imei";
    public static final String IMSI = "imsi";
    public static final String MACADDRESS = "mac_address";
    private static final String DEVICEID_STORE = "deviceId_store";
    private static final String DEVICEID = "deviceId";
    private static final String TAG = "PhoneUtil";
    private static String phone_imei = "";
    private static String phone_imsi = "";
    private static String phone_wifiaddr = "";
    private static String mDeviceId;
    private static Future<String> mLastFuture;

    public PhoneUtil() {
    }

    private static String generateImei() {
        StringBuilder imei = new StringBuilder();
        long time = System.currentTimeMillis();
        String currentTime = Long.toString(time);
        imei.append(currentTime.substring(currentTime.length() - 5));
        StringBuilder model = new StringBuilder();
        model.append(Build.MODEL.replaceAll(" ", ""));

        while (model.length() < 6) {
            model.append('0');
        }

        imei.append(model.substring(0, 6));
        Random random = new Random(time);

        long tmp;
        for (tmp = 0L; tmp < 4096L; tmp = random.nextLong()) {
            ;
        }

        imei.append(Long.toHexString(tmp).substring(0, 4));
        return imei.toString();
    }

    @SuppressLint("MissingPermission")
    public static String getImei(Context context) {
        if (!TextUtils.isEmpty(phone_imei)) {
            return phone_imei;
        } else {
            String imei = null;
            SharedPreferences sp = context.getSharedPreferences("imei", 0);
            imei = sp.getString("imei", (String) null);
            if (imei == null || imei.length() == 0) {
                try {
                    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                    imei = tm.getDeviceId();
                } catch (Exception var4) {
                    var4.printStackTrace();
                }

                if (imei == null || imei.length() == 0) {
                    imei = generateImei();
                }

                for (imei = imei.replaceAll(" ", "").trim(); imei.length() < 15; imei = "0" + imei) {
                    ;
                }

                SharedPreferences.Editor editor = sp.edit();
                editor.putString("imei", imei);
                if (Build.VERSION.SDK_INT >= 9) {
                    editor.apply();
                } else {
                    editor.commit();
                }
            }

            phone_imei = imei.trim();
            return phone_imei;
        }
    }

    @SuppressLint("MissingPermission")
    public static String getImsi(Context context) {
        if (!TextUtils.isEmpty(phone_imsi)) {
            return phone_imsi;
        } else {
            String imsi = null;
            SharedPreferences sp = context.getSharedPreferences("imei", 0);
            imsi = sp.getString("imsi", (String) null);
            if (imsi == null || imsi.length() == 0) {
                try {
                    TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
                    imsi = tm.getSubscriberId();
                } catch (Exception var4) {
                    var4.printStackTrace();
                }

                if (imsi == null || imsi.length() == 0) {
                    imsi = generateImei();
                }

                for (imsi = imsi.replaceAll(" ", "").trim(); imsi.length() < 15; imsi = "0" + imsi) {
                    ;
                }

                SharedPreferences.Editor editor = sp.edit();
                editor.putString("imsi", imsi);
                if (Build.VERSION.SDK_INT >= 9) {
                    editor.apply();
                } else {
                    editor.commit();
                }
            }

            phone_imsi = imsi;
            return phone_imsi;
        }
    }

    public static String getLocalMacAddress(Context context) {
        if (!TextUtils.isEmpty(phone_wifiaddr)) {
            return phone_wifiaddr;
        } else {
            WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
            WifiInfo info = wifi.getConnectionInfo();
            String wifiaddr = info.getMacAddress();
            SharedPreferences sp;
            if (wifiaddr != null && !"".equals(wifiaddr)) {
                sp = context.getSharedPreferences("mac_address", 0);
                SharedPreferences.Editor editor = sp.edit();
                editor.putString("mac_address", wifiaddr);
                if (Build.VERSION.SDK_INT >= 9) {
                    editor.apply();
                } else {
                    editor.commit();
                }
            } else {
                sp = context.getSharedPreferences("mac_address", 0);
                wifiaddr = sp.getString("mac_address", "");
            }

            phone_wifiaddr = wifiaddr;
            return phone_wifiaddr;
        }
    }

    public static String getOriginalImei(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String imei = tm.getDeviceId();
        if (imei != null) {
            imei = imei.trim();
        }

        return imei;
    }

    public static String getOriginalImsi(Context context) {
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        @SuppressLint("MissingPermission") String imsi = tm.getSubscriberId();
        if (imsi != null) {
            imsi = imsi.trim();
        }

        return imsi;
    }

    public static String getSerialNum() {
        String serialnum = null;

        try {
            Class<?> c = Class.forName("android.os.SystemProperties");
            Method get = c.getMethod("get", String.class, String.class);
            serialnum = (String) ((String) get.invoke(c, "ro.serialno", "unknown"));
        } catch (Exception var3) {
            ;
        }

        return serialnum;
    }

    public static String getAndroidId(Context context) {
        return Settings.Secure.getString(context.getContentResolver(), "android_id");
    }

    public static void clear(Context context) {
        mDeviceId = null;
        deleteDeviceID(context);
    }

    private static String getRemoteDeviceID(Context context) {
        String s0 = MD5Util.md5AsBase64(Build.BRAND + Build.MODEL);
        String s1 = MD5Util.md5AsBase64(getLocalMacAddress(context) + getAndroidId(context) + getSerialNum());
        String s2 = MD5Util.md5AsBase64(getImei(context) + getImsi(context));
        StringBuilder deviceId = new StringBuilder();
        deviceId.append(s0.substring(4, 16));
        deviceId.append(s1.substring(8, 24));
        deviceId.append(s2.substring(16));
        saveDeviceID(context, deviceId.toString());
        return deviceId.toString();
    }

    private static String getSharePreferenceDeviceID(Context context) {
        if (context != null) {
            SharedPreferences storage = context.getSharedPreferences("deviceId_store", 0);
            String deviceInfo = storage.getString("deviceId", "");
            if (!TextUtils.isEmpty(deviceInfo)) {
                return deviceInfo;
            }
        }

        return null;
    }

    private static void saveDeviceID(Context context, String deviceId) {
        if (!TextUtils.isEmpty(deviceId)) {
            mDeviceId = deviceId;
            SharedPreferences storage = context.getSharedPreferences("deviceId_store", 0);
            storage.edit().putString("deviceId", deviceId).apply();
            saveDeviceIDInLocalFile(mDeviceId);
            saveDeviceIDInLocalFile1(context, mDeviceId);
        }
    }

    public static void saveDeviceIDInLocalFile(String deviceId) {
        boolean sdCardExist = Environment.getExternalStorageState().equals("mounted");
        if (sdCardExist) {
            File sdDir = Environment.getExternalStorageDirectory();
            FileUtil.writeData(getSDcardDeviceIdSavePath(sdDir.getPath()), deviceId);
        }

    }

    public static void saveDeviceIDInLocalFile1(Context context, String deviceId) {
        File externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_DCIM);
        if (externalFilesDir != null) {
            FileUtil.writeData(getSDcardDeviceIdSavePath(externalFilesDir.getPath()), deviceId);
        }
    }

    private static void deleteDeviceID(Context context) {
        SharedPreferences storage = context.getSharedPreferences("deviceId_store", 0);
        storage.edit().putString("deviceId", "").apply();
    }

    public static String getDeviceID(Context context) {
        if (TextUtils.isEmpty(mDeviceId)) {
            if (TextUtils.isEmpty(mDeviceId)) {
                mDeviceId = getSharePreferenceDeviceID(context);
            }

            if (TextUtils.isEmpty(mDeviceId)) {
                mDeviceId = getDeviceIDFromLocalFile1(context);
            }

            if (TextUtils.isEmpty(mDeviceId)) {
                mDeviceId = getDeviceIDFromLocalFile(context);
            }

            if (TextUtils.isEmpty(mDeviceId)) {
                mDeviceId = getRemoteDeviceID(context);
            }
        }

        return mDeviceId;
    }

    public static String getDeviceIDFromLocalFile1(Context context) {
        File externalFilesDir = context.getExternalFilesDir(Environment.DIRECTORY_DCIM);
        return externalFilesDir == null ? null : getDeviceIDFromLocalFile(externalFilesDir);
    }

    public static String getDeviceIDFromLocalFile(Context context) {
        boolean sdCardExist = Environment.getExternalStorageState().equals("mounted");
        if (sdCardExist) {
            File sdDir = Environment.getExternalStorageDirectory();
            return getDeviceIDFromLocalFile(sdDir);
        } else {
            return null;
        }
    }

    public static String getDeviceIDFromLocalFile(File file) {
        String sDcardDeviceIdSavePath = getSDcardDeviceIdSavePath(file.getPath());
        return FileUtil.readFileByLine(new File(sDcardDeviceIdSavePath), 0, 1);
    }

    public static String getSDcardDeviceIdSavePath(String filePath) {
        return filePath + File.separator + ".dimcc";
    }

    public static String getLocalDeviceID(Context context) {
        if (TextUtils.isEmpty(mDeviceId)) {
            mDeviceId = getSharePreferenceDeviceID(context);
        }

        return mDeviceId;
    }
}